package com.bigshark.amine_admin.sys.controller;

import com.bigshark.amine_admin.common.annocation.SysLog;
import com.bigshark.amine_admin.sys.model.SysConfig;
import com.bigshark.amine_admin.sys.service.SysConfigService;
import com.bigshark.common.utills.PageUtils;
import com.bigshark.common.utills.R;
import com.bigshark.common.utills.ValidatorUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/sys/config")
public class SysConfigController {
    @Autowired
    SysConfigService configService;

    @SysLog("添加配置")
    @PostMapping("/")
    public R create(@RequestBody SysConfig config){
        ValidatorUtils.validateEntity(config);
        configService.save(config);
        return R.ok();
    }

    @SysLog("删除配置")
    @DeleteMapping("/{id}")
    public R del(@PathVariable("id")Long id){
        if( !configService.removeById(id)){
            return R.error("删除失败");
        }
        return R.ok();
    }

    @SysLog("修改配置配置")
    @PutMapping("/{id}")
    public R upData(@PathVariable("id")Long id,@RequestBody SysConfig config){
        config.setId(id);
        if(!configService.saveOrUpdate(config)){
            return R.error("修改失败");
        }
        return R.ok();
    }

    @GetMapping("/{id}")
    public R getById(@PathVariable("id") Long id){
        SysConfig data = configService.getById(id);
        return  R.ok().put("data",data);
    }

    @GetMapping("/")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = configService.queryList(params);
        return R.ok().put("data",page);
    }
}
